package com.bookmall.cart.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bookmall.cart.domain.Cart;
import com.bookmall.cart.domain.CartItem;
import com.bookmall.cart.domain.vo.BookInfoVo;
import com.bookmall.cart.feign.ProductFeignService;
import com.bookmall.cart.interceptor.CartInterceptor;
import com.bookmall.cart.service.ICartService;
import com.bookmall.common.entity.to.MemberInfoTo;
import com.bookmall.common.entity.to.UserInfoTo;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-04-15-14:15
 */
@Controller
public class CartController {
    @Autowired
    private ProductFeignService productFeignService;

    @Autowired
    private ICartService cartService;

    /**
     * 放置一个购物车对象
     * @author 胡菜鸡
     * @date 2022/4/15 14:18
     */
    @GetMapping("/cart.html")
    public String cart(Model model) {
        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cart";
    }

    /**
     * 添加购物项到购物车，成功跳转至添加成功页面
     * @author 胡菜鸡
     * @date 2022/4/19 15:11
     */
    @GetMapping("/addToCart.html")
    public String addToCart(@RequestParam("bookId") String bookId,
                            @RequestParam("count") Integer count,
                            RedirectAttributes ra){
        cartService.addToCart(bookId, count);
        ra.addAttribute("bookId", bookId);
        return "redirect:http://huyulu666.top/addToCartSuccess.html";
    }

    /**
     * 进入添加购物车成功页面
     * @author 胡菜鸡
     * @date 2022/4/20 8:55
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("bookId") String bookId, Model model){
        //重定向到成功页面，再次查询购物车数据即可
        CartItem item = cartService.getCartItem(bookId);
        model.addAttribute("cartItem", item);
        return "success";
    }

    /**
     * 改变书籍选中状态
     * @author 胡菜鸡
     * @date 2022/4/20 10:01
     */
    @GetMapping("/checkedItem")
    @ResponseBody
    public CartItem checkedItem(@RequestParam("bookId") String bookId,
                              @RequestParam("check") Integer check){
        CartItem cartItem = cartService.checkItem(bookId, check);
        return cartItem;
    }

    /**
     * 改变书籍数量
     * @author 胡菜鸡
     * @date 2022/4/20 10:01
     */
    @GetMapping("/countItem")
    @ResponseBody
    public CartItem countItem(@RequestParam("bookId") String bookId,
                                @RequestParam("count") Integer count){
        CartItem cartItem = cartService.countItem(bookId, count);
        return cartItem;
    }

    /**
     * 删除购物车商品项
     * @author 胡菜鸡
     * @date 2022/4/20 10:57
     */
    @GetMapping("/deleteItem")
    @ResponseBody
    public Long deleteItem(@RequestParam("bookId") String bookId){
        return cartService.deleteItem(bookId);
    }

    /**
     * 获取用户购物车中的数据
     * @author 胡菜鸡
     * @date 2022/4/21 18:01
     */
    @GetMapping("/currentUserCartItems")
    public List<CartItem> getCurrentUserCartItems(){
        return cartService.getUserCartItems();
    }

}
